package console

import (
	"fmt"
	"log"

	tui "github.com/marcusolsson/tui-go"
)

var (
	progress *tui.Progress
	title    *tui.Label
	library  *tui.Table
	status   *tui.StatusBar
	root     *tui.Box
	ui       tui.UI
)

func ConsoleView() {
	main_drawing()

	main_key()

	if err := ui.Run(); err != nil {
		log.Fatal(err)
	}
}

//绘制主界面
func main_drawing() {
	progress = tui.NewProgress(100)

	title = tui.NewLabel("WIKIHepler_PCR")

	library = tui.NewTable(0, 0)

	library.SetColumnStretch(0, 1)
	library.SetColumnStretch(1, 1)
	library.SetColumnStretch(2, 4)
	library.SetFocused(true)

	library.AppendRow(
		tui.NewLabel("活动日历数据"),
		tui.NewLabel(""),
		tui.NewLabel("进行活动日历数据的管理"),
	)

	library.AppendRow(
		tui.NewLabel("快速导入活动日历数据"),
		tui.NewLabel(""),
		tui.NewLabel("利用文件快速导入日历数据"),
	)

	library.AppendRow(
		tui.NewLabel("命令模式"),
		tui.NewLabel(""),
		tui.NewLabel("利用命令解析快速完成复杂操作"),
	)

	library.AppendRow(
		tui.NewLabel("退出"),
		tui.NewLabel(""),
		tui.NewLabel("退出这个傻憨憨的系统"),
	)

	status = tui.NewStatusBar("")
	status.SetText("按下F1获取帮助")
	status.SetPermanentText(`作者：DeiMOE`)

	root = tui.NewVBox(
		title,
		library,
		tui.NewSpacer(),
		progress,
		status,
	)

	if ui != nil {
		ui.SetWidget(root)
		ui.Repaint()
	} else {
		ui, _ = tui.New(root)
	}

}

//主界面快捷键绑定
func main_key() {
	ui.ClearKeybindings()
	ui.SetKeybinding("F1", main_helper)
	ui.SetKeybinding("Enter", main_enterPress)
	ui.SetKeybinding("Esc", func() { ui.Quit() })
	ui.SetKeybinding("F5", func() { ui.Repaint() })
}

//主界面快捷键解析
func main_enterPress() {
	op := library.Selected()
	status.SetPermanentText(fmt.Sprintf("%d", op))

	switch op {
	case 0:
		event_drawing()
	case 1:
		import_drawing()
	case 3:
		ui.Quit()
	}

}

//主界面帮助系统
func main_helper() {
	title.SetText("系统帮助")
}

//界面退回
func main_back() {
	main_drawing()
	main_key()
}
